package br.eco.eagua.view;

import br.eco.eagua.R;
import br.eco.eagua.dao.DatabaseHelper;
import br.eco.eagua.dao.ProjetoDAO;
import br.eco.eagua.model.Usuario;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CadastroUsuarioController extends Activity implements OnClickListener{
	
	Intent nova;
	private EditText login;
	private EditText senha;
	private EditText confirmaSenha;
	private Button novoCadastro;
	
	private Usuario usuario;
	private DatabaseHelper helper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cadastrousuario);
		helper = new DatabaseHelper(this);
		
		login = (EditText) findViewById(R.id.novoLogin);
		senha = (EditText) findViewById(R.id.novaSenha);
		confirmaSenha = (EditText) findViewById(R.id.confirmaSenha);
		novoCadastro = (Button) findViewById(R.id.botaoNovoUsuario);
		
		novoCadastro.setOnClickListener(this);		
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.botaoNovoUsuario:
			ProjetoDAO dao = new ProjetoDAO(helper);
			String verLogin = login.getText().toString().trim();
			
			if(dao.verificarLogin(verLogin) == true){
				Toast.makeText(CadastroUsuarioController.this, "LOGIN ESTA INDISPONIVEL", Toast.LENGTH_SHORT).show();
				
				
			}else{					
			
			String verSenha = senha.getText().toString().trim();
			String verConfSenha = confirmaSenha.getText().toString().trim();
			
			
			
				if(verSenha.equals(verConfSenha)){				
					usuario = new Usuario(verLogin, verSenha);					
					dao.cadastrarUsuario(usuario);
					Toast.makeText(CadastroUsuarioController.this, "CADASTRADO COM SUCESSO", Toast.LENGTH_SHORT).show();
					nova = new Intent(this, MenuController.class);
					startActivity(nova);
					
				}else{
					
					Toast.makeText(CadastroUsuarioController.this, "SENHA NAO CONFERE", Toast.LENGTH_SHORT).show();
				}
			}	
			break;

		default:
			break;
		}
		
	}

}
